package com.conn;

import java.io.*;
import java.util.*;
import java.net.*;
import com.conn.*;

//class ClientInfo {
//    Socket cli;
//    public ClientInfo (Socket s) {
//       cli = s;
//    }
//}

public class Server {
    private static Server s;
    private ServerSocket ss;
    public static Parser parser;
    public static Packer packer;
    public static Store store;
    //ArrayLi, parserst <ClientInfo> sList = new ArrayList <ClientInfo>();
    
    static {
        try {
            s = new Server();
            parser = new Parser1();
            packer = new Packer1();
            store = new LocalSimpleStore();
        } catch (IOException ex) {
            ex.printStackTrace();
        }
    }

    private Server() throws IOException {
        ss = new ServerSocket(GlobalData.getServerPort());
        System.out.println("create server socket with port "+
                GlobalData.getServerPort()+" ok!");
    }

    public static Server getServer() {
        return s;
    }

    public void start() throws IOException {
        while (true) {
            Socket newsd = ss.accept();
            System.out.println("one client connected with ip: "+
                    newsd.getInetAddress().getHostAddress()+" port:"+newsd.getPort());
            //sList.add(newsd);

            //new Thread(new Work(newsd)).start();
            new ServerWorker(newsd).start();
            //sometimes, we need check sList and remove unuse Socket.
        }
    }
}

